package com.chance.cc.crawler.development.command.meta.keyword.medical;

import com.chance.cc.crawler.development.command.meta.keyword.CrawlerDomainKeywordCommand;
import com.chance.cc.crawler.meta.core.bean.crawler.CrawlerDomainKeys;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author lt
 * @version 1.0
 * @date 2021-03-15 10:19:55
 * @email okprog@sina.com
 */
public class MedicalKeywordsCommonScript {
    private static final String kwSite = "medical_keyword";
    private static final String TieBaKwSite = "medical_tieba_keyword";
    public static final String authorSite = "medical_weixin_uid";

    private static CrawlerDomainKeywordCommand crawlerDomainKeywordCommand =
            new CrawlerDomainKeywordCommand("192.168.1.215", 9599);

    public static void main(String[] args) {
//        String filepath = "crawler-scripts-command/src/main/java/com/chance/cc/crawler/development/command/meta/keyword/medical/medical_tieba_kw.txt";
        String filepath = "crawler-scripts-command/src/main/java/com/chance/cc/crawler/development/command/meta/keyword/medical/medical_kw.txt";
//        String filepath = "crawler-scripts-command/src/main/java/com/chance/cc/crawler/development/command/meta/keyword/medical/medical_weixin_uid.txt";
//        System.out.println("domain:");
//        Scanner sc = new Scanner(System.in);
//        final String domain = sc.next();
        List<String> strings = Arrays.asList("weibo", "weixin", "dxy", "toutiao", "bilibili", "baidu");
        for (String string : strings) {
            writeKeys2MySql(string,kwSite,filepath);
        }
    }

    @Test
    public void addAdhocKws(){
        crawlerDomainKeywordCommand =
                new CrawlerDomainKeywordCommand("192.168.1.217", 9599);
        String filepath = "C:\\Users\\Administrator\\Documents\\chance\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\meta\\keyword\\medical\\medical_kw_adhoc.txt";
        String kwSite = "medical_adhoc_keyword";
        System.out.println("domain:");
        Scanner sc = new Scanner(System.in);
        final String domain = sc.next();
        List<String> strings = Arrays.asList("weibo", "weixin", "dxy", "toutiao", "bilibili", "baidu");
        for (String string : strings) {
            writeKeys2MySql(string,kwSite,filepath);
        }
    }

    public static void writeKeys2MySql(String domain,String site, String filepath){
        List<String> content = getContent(filepath);
        Set<String> dupList = new HashSet<>();
        for (String keyword : content) {
            System.out.println(keyword);
            dupList.add(keyword);
        }
        System.out.println(dupList.size());
        for (String keyword : dupList) {
            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain(domain);
            crawlerDomainKeys.setSite(site);
            crawlerDomainKeys.setKeyword(keyword);
            crawlerDomainKeys.setStatus(0);
            crawlerDomainKeywordCommand.addOrUpdateKeys(crawlerDomainKeys);
//            crawlerDomainKeywordCommand.delKeys(crawlerDomainKeys);
        }
    }

    public static List<String> getContent(String filepath) {
        //读取文件
        List<String> lineLists = null;
        try {
            System.out.println(filepath);
            lineLists = Files
                    .lines(Paths.get(filepath), Charset.defaultCharset())
                    .flatMap(line -> Arrays.stream(line.split("\n")))
                    .collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return lineLists;
    }
}
